package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.service.ICategoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@RestController
@RequestMapping("/category")
public class CategoryController {

    @Resource
    private ICategoryService categoryService;

    @GetMapping("/page")
    public ResultInfo findByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        // 1.接收参数
        // 2.调用service查询
        Page<Category> page = categoryService.findByPage(pageNum, pageSize);

        // 3.返回结果
        return ResultInfo.success(page);
    }

    @PostMapping
    public ResultInfo save(@RequestBody Category category) {
        //  1.接收请求体参数
        // 2.调用service新增
        categoryService.saveCategory(category);
        // 3.返回结果
        return ResultInfo.success(null);
    }

    //修改分类
    @PutMapping("")
    public ResultInfo update(@RequestBody Category category) { // 1.接收参数
        // 2.调用serivce修改
        categoryService.updateCategory(category);

        // 3.返回resultInfo结果
        return ResultInfo.success(null);
    }

    // 根据type查询分类列表
    @GetMapping("/list")
    public ResultInfo categoryList(Integer type) { // 1.接收请求参数
        // 2.调用service查询
        List<Category> list = categoryService.findByType(type);
        // 3.返回结果
        return ResultInfo.success(list);

    }
}

